package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import model.Beneficiario;
import model.DuplicataReceber;

public class DuplicataReceberJDBCCadastro implements
		DuplicataReceberCadastroDAO {

	Connection conexao;
	static public double vlbase = 0;

	public DuplicataReceberJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(DuplicataReceber duplicatareceber) {

		String sqlquery = "select TP.CDTPSERVICO CDTPSERVICO, TP.VLBASE VLBASE, CS.CDCLIENTE_SERVICO CDCLIENTE_SERVICO "
				+ "from CLIENTE_SERVICO CS, TPSERVICO TP, SERVICO S "
				+ "Where CS.CDCLIENTE = "
				+ duplicatareceber.getCdcliente()
				+ " AND TP.CDTPSERVICO = S.CDTPSERVICO "
				+ "AND S.CDSERVICO = CS.CDSERVICO AND TP.NMTPSERVICO LIKE 'PAGAMENTO CLI%'; ";
		Double vlduplicata = null;
		String cdcliente_servico = "";
		ResultSet rs = null;
		try {
			java.sql.Statement stmt = conexao.createStatement();
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				vlbase = Double.parseDouble(rs.getString("VLBASE"));
				vlduplicata = Double.parseDouble(rs.getString("VLBASE"));
				cdcliente_servico = rs.getString("CDCLIENTE_SERVICO");
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}

		sqlquery = "insert into duplicata_receber(CDCLIENTE, CDCLIENTE_SERVICO, VLTDUPLICATA, DTDUPLICATA, DTVENCIMENTO, BOLSTATUS)"
				+ " values(?,?,?,?,?,?);";
		PreparedStatement pstmt;
		String valor = "";
		try {
			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, duplicatareceber.getCdcliente());
			pstmt.setString(2, cdcliente_servico);
			pstmt.setDouble(3, vlduplicata * 6);
			Date datenow = null;

			try {
				String date = new SimpleDateFormat("dd/MM/yyyy")
						.format(new Date());
				SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
				datenow = dateFormat.parse(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
			pstmt.setDate(4, new java.sql.Date(datenow.getTime()));
			pstmt.setDate(5, new java.sql.Date(datenow.getTime()));
			pstmt.setString(6, "A");
			pstmt.execute();

			sqlquery = "";
			sqlquery = "Select CDDUPLICATA_RECEBER from DUPLICATA_RECEBER where CDCLIENTE = "
					+ duplicatareceber.getCdcliente()
					+ " and BOLSTATUS = 'A' and DTDUPLICATA = '"
					+ new java.sql.Date(datenow.getTime()) + "';";

			try {
				rs = null;
				java.sql.Statement stmt = conexao.createStatement();
				rs = stmt.executeQuery(sqlquery);
				while (rs.next()) {
					valor = rs.getString("CDDUPLICATA_RECEBER");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return ("error");
			}

			return (valor);
		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public String atualizar(List<DuplicataReceber> duplicataReceberLista) {
		String sqlquery = "update DUPLICATA_RECEBER set BOLSTATUS = ?, DTPAGAMENTO = ? "
				+ "where CDDUPLICATA_RECEBER = ? ;";

		PreparedStatement pstmt;
		int tamanho = 0;
		int tam = duplicataReceberLista.size();
		Date datenow = null;
		try {
			String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			datenow = dateFormat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}

		while (tamanho <= (tam - 1)) {
			try {
				pstmt = this.conexao.prepareStatement(sqlquery);

				if (duplicataReceberLista.get(tamanho).getBolstatus()
						.equals("true")) {
					pstmt.setString(1, "Q");
				} else {
					pstmt.setString(1, "A");
				}
				pstmt.setDate(2, new java.sql.Date(datenow.getTime()));
				pstmt.setString(3, duplicataReceberLista.get(tamanho)
						.getCdduplicatareceber());
				pstmt.execute();

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("VALOR EM BRANCO " + sqlquery
						+ " ++++++++++++++ ");
				System.out.println("TAMANHO " + tamanho + " ++++++++++++++ ");
				return ("error");
			}
			tamanho++;
		}
		duplicataReceberLista.clear();
		return "success";
	}

	public ResultSet editarConsulta(String idcliente) {

		String sql = "select CDCLIENTE, CDDUPLICATA_RECEBER, VLTDUPLICATA, DATE_FORMAT(DTDUPLICATA, '%d/%m/%Y') as DTDUPLICATA, "
				+ "DATE_FORMAT(DTVENCIMENTO,'%d/%m/%Y') as DTVENCIMENTO, BOLSTATUS "
				+ "from duplicata_receber where CDCLIENTE = "
				+ idcliente
				+ " order by DTDUPLICATA;";
		ResultSet value = null;
		try {
			java.sql.Statement stmt = conexao.createStatement();
			value = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	public void editar(DuplicataReceber duplicatareceber) {
	}

	public void excluir(DuplicataReceber duplicatareceber) {
	}

	public List<DuplicataReceber> ListarDuplicataReceberT() {
		return null;
	}

}
